﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

namespace EHTWebsite.Code
{
    /// <summary>
    /// Author: chiennv
    /// CreatedDate: 12/12/2011
    /// Description: provider Khách Hàng, Đối tác
    /// </summary>
    public class KhachHangProvider
    {
        /// <summary>
        /// Author: chiennv
        /// CreatedDate: 12/12/2011
        /// Description: Insert, Update Khách Hàng
        /// </summary>
        /// <param name="theKhachHang">Đối tượng khách hàng</param>
        /// <param name="IsAdd">Thêm mới</param>
        public static Int32 IUKhachHang(KhachHang theKhachHang, bool IsAdd)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_IUKhachHang";
            command.CommandType = CommandType.StoredProcedure;

            SqlParameter theKhachHangParameter = new SqlParameter();

            theKhachHangParameter.ParameterName = "@CustomerIDOut";
            theKhachHangParameter.Direction = ParameterDirection.Output;
            theKhachHangParameter.DbType = DbType.Int32;

            command.Parameters.Add(theKhachHangParameter);

            command.Parameters.Add(new SqlParameter("@CustomerID", theKhachHang.CustomerID));
            command.Parameters.Add(new SqlParameter("@CustomerVN", theKhachHang.CustomerVN));
            command.Parameters.Add(new SqlParameter("@CustomerOrder", theKhachHang.CustomerOrder));
            command.Parameters.Add(new SqlParameter("@AddressVN", theKhachHang.AddressVN));
            command.Parameters.Add(new SqlParameter("@AddressOther", theKhachHang.AddressOther));
            command.Parameters.Add(new SqlParameter("@Phone", theKhachHang.Phone));
            command.Parameters.Add(new SqlParameter("@Email", theKhachHang.Email));
            command.Parameters.Add(new SqlParameter("@CustomerIMG", theKhachHang.CustomerIMG));
            command.Parameters.Add(new SqlParameter("@Publish", theKhachHang.Publish));
            command.Parameters.Add(new SqlParameter("@NumberOfOrder", theKhachHang.NumberOfOrder));
            command.Parameters.Add(new SqlParameter("@CustomerURL", theKhachHang.CustomerURL));       
            command.Parameters.Add(new SqlParameter("@IsAdd", IsAdd));

            DataProvider.ExecuteNoneQuery(command);

            return Int32.Parse(command.Parameters["@CustomerIDOut"].Value.ToString());
        }

        /// <summary>
        /// Author: chiennv       
        /// Description: Xóa Khách Hàng
        /// </summary>
        /// <param name="KhachHangID">Mã khách hàng</param>
        public static void DeleteKhachHangByKhachHangID(Int32 CustomerID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_DeleteKhachHangByKhachHangID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@CustomerID", CustomerID));

            DataProvider.ExecuteNoneQuery(command);
        }


        /// <summary>
        /// Author: chiennv       
        /// Description: thay doi trang thai Khách Hàng
        /// </summary>
        /// <param name="KhachHangID">Mã khách hàng</param>
        public static void ChangePublishKhachHangByKhachHangID(Int32 CustomerID, Boolean Publish)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_ChangePublishByKhachHangID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@CustomerID", CustomerID));
            command.Parameters.Add(new SqlParameter("@Publish", Publish));

            DataProvider.ExecuteNoneQuery(command);
        }

        /// <summary>
        /// Author: chiennv
        /// Description: Lấy danh sách Khách Hàng có phân trang
        /// </summary>
        /// <param name="theKhachHang">Đối tượng khách hàng chứa các khóa lọc</param>
        /// <param name="pageIndex">Trang hiện tại</param>
        /// <param name="pageSize">Số record trên 1 trang</param>
        /// <param name="totalRecord">Tông số record tìm thấy</param>
        /// <returns>Danh sách Khách Hàng</returns>
        public static DataSet GetKhachHangsByFillter(KhachHang theKhachHang, Int32 pageIndex, Int32 pageSize, out Int32 totalRecord)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetKhachHangsByFillter";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@CustomerID", theKhachHang.CustomerID));
            command.Parameters.Add(new SqlParameter("@CustomerVN", theKhachHang.CustomerVN));
            command.Parameters.Add(new SqlParameter("@CustomerOrder", theKhachHang.CustomerOrder));            
            command.Parameters.Add(new SqlParameter("@PageIndex", pageIndex));
            command.Parameters.Add(new SqlParameter("@PageSize", pageSize));
            DataSet ds = DataProvider.ExecuteDataSet(command, out totalRecord);
            return ds;
        }

        /// <summary>
        /// Author: chiennv       
        /// Description: Lấy đối tượng Khách Hàng bằng Mã khách hàng
        /// </summary>
        /// <param name="KhachHangID">Mã khách hàng</param>
        /// <returns>Dối tượng Khách Hàng</returns>
        public static KhachHang GetKhachHangByKhachHangID(Int32 CustomerID)
        {
            SqlCommand command = new SqlCommand();
            command.CommandText = "proc_GetKhachHangByKhachHangID";
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add(new SqlParameter("@CustomerID", CustomerID));

            DataSet ds = DataProvider.ExecuteDataSet(command);
            KhachHang theKhachHangNew = new KhachHang();

            if (ds.Tables[0].Rows.Count > 0)
            {
                theKhachHangNew = new KhachHang();
                theKhachHangNew.CustomerVN = ds.Tables[0].Rows[0]["CustomerVN"].ToString();
                theKhachHangNew.CustomerOrder = ds.Tables[0].Rows[0]["CustomerOrder"].ToString();
                theKhachHangNew.NumberOfOrder = Int32.Parse(ds.Tables[0].Rows[0]["NumberOfOrder"].ToString());
                theKhachHangNew.Publish = Boolean.Parse(ds.Tables[0].Rows[0]["Publish"].ToString());
                theKhachHangNew.AddressVN = ds.Tables[0].Rows[0]["AddressVN"].ToString();
                theKhachHangNew.AddressOther = ds.Tables[0].Rows[0]["AddressOther"].ToString();              
                theKhachHangNew.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                theKhachHangNew.Email = ds.Tables[0].Rows[0]["Email"].ToString();
                theKhachHangNew.CustomerIMG = ds.Tables[0].Rows[0]["CustomerIMG"].ToString();
                theKhachHangNew.CustomerURL = ds.Tables[0].Rows[0]["CustomerURL"].ToString();
                theKhachHangNew.CustomerID = Int32.Parse(ds.Tables[0].Rows[0]["CustomerID"].ToString());
            }

            return theKhachHangNew;
        }
    }
}